package com.wh.base.sys.log.controller;


import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.base.sys.log.controller.form.IdForm;
import com.wh.base.sys.log.controller.form.SearchListForm;
import com.wh.base.sys.log.controller.form.SearchPageForm;
import com.wh.base.sys.log.entity.Log;
import com.wh.base.sys.log.service.LogService;
import com.wh.base.util.R;
import com.wh.base.util.RedisUtil;
import com.wh.base.util.StringUtil;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 日志 前端控制器
 * </p>
 *
 * @author WangHong
 * @since 2022-05-07
 */
@Controller
@RequestMapping("/sys/log")
public class LogController {

	@Autowired
	private LogService service;
	
	@Resource
    private RedisUtil redisUtil;

	@GetMapping("/index")
	@ApiOperation(value = "首页页面")
	@SaCheckPermission("log:view")
	public String index(Model model){
		model.addAttribute("authSign", (Map<String,String>)redisUtil.get("auth_sign_"+StpUtil.getLoginIdAsLong())); //用户权限
		return "log/index";
	}

	@PostMapping("/getPage")
	@ApiOperation(value = "分页查询数据")
	@ResponseBody
	@SaCheckLogin
	public R getPage(@Validated SearchPageForm form){
		Page roadPage = new Page<>(form.getPageNum(), form.getPageSize());
		LambdaQueryWrapper<Log> queryWrapper = new QueryWrapper().lambda();
		
		if(!StringUtil.isEmpty(form.getIp())) {
			queryWrapper.like(Log::getIp, form.getIp());
		}
		if(!StringUtil.isEmpty(form.getUri())) {
			queryWrapper.like(Log::getUri, form.getUri());
		}
		if(!StringUtil.isEmpty(form.getOperModule())) {
			queryWrapper.like(Log::getOperModule, form.getOperModule());
		}
		if(!StringUtil.isEmpty(form.getOperType())) {
			queryWrapper.like(Log::getOperType, form.getOperType());
		}
		if(!StringUtil.isEmpty(form.getOperMethod())) {
			queryWrapper.like(Log::getOperMethod, form.getOperMethod());
		}
		if(!StringUtil.isEmpty(form.getOperDesc())) {
			queryWrapper.like(Log::getOperDesc, form.getOperDesc());
		}
		if(!StringUtil.isEmpty(form.getRequestState())) {
			queryWrapper.eq(Log::getRequestState, form.getRequestState());
		}
		
		if(!StringUtil.isEmpty(form.getStartTime())) {
			queryWrapper.ge(Log::getCreateTime, form.getStartTime());
		}
		if(!StringUtil.isEmpty(form.getEndTime())) {
			queryWrapper.le(Log::getCreateTime, form.getEndTime());
		}
		queryWrapper.orderByDesc(Log::getCreateTime);
		Page pageList = service.page(roadPage, queryWrapper);
		return R.ok().put("page", pageList);
	}

	@PostMapping("/getList")
	@ApiOperation(value = "查询所有数据")
	@ResponseBody
	@SaCheckLogin
	public R getList(@Validated SearchListForm form){
		LambdaQueryWrapper<Log> queryWrapper = new QueryWrapper().lambda();
		if(!StringUtil.isEmpty(form.getIp())) {
			queryWrapper.like(Log::getIp, form.getIp());
		}
		if(!StringUtil.isEmpty(form.getUri())) {
			queryWrapper.like(Log::getUri, form.getUri());
		}
		if(!StringUtil.isEmpty(form.getOperModule())) {
			queryWrapper.like(Log::getOperModule, form.getOperModule());
		}
		if(!StringUtil.isEmpty(form.getOperType())) {
			queryWrapper.like(Log::getOperType, form.getOperType());
		}
		if(!StringUtil.isEmpty(form.getOperMethod())) {
			queryWrapper.like(Log::getOperMethod, form.getOperMethod());
		}
		if(!StringUtil.isEmpty(form.getOperDesc())) {
			queryWrapper.like(Log::getOperDesc, form.getOperDesc());
		}
		if(!StringUtil.isEmpty(form.getRequestState())) {
			queryWrapper.eq(Log::getRequestState, form.getRequestState());
		}
		if(!StringUtil.isEmpty(form.getStartTime())) {
			queryWrapper.ge(Log::getCreateTime, form.getStartTime());
		}
		if(!StringUtil.isEmpty(form.getEndTime())) {
			queryWrapper.le(Log::getCreateTime, form.getEndTime());
		}
		queryWrapper.orderByDesc(Log::getCreateTime);
		return R.ok().put("list", service.list(queryWrapper));
	}

	

	@PostMapping("/getById")
	@ApiOperation(value = "根据id查询数据")
	@SaCheckPermission("log:view")
	@ResponseBody
	public R getById(@Validated IdForm form){
		return R.ok().put("obj", service.getById(form.getId()));
	}
}
